import { useLocation, history, Outlet } from "umi";
import { Result, Button } from "antd";

//白名单页面
const whiteList = ["dictionary/detail/"];

const PageAuth = ({ menu }) => {
  const { pathname } = useLocation();
  const extractPathsFromHierarchy = (hierarchy) => {
    const paths = [];

    function traverse(node, currentPath) {
      const path = currentPath + node.path;

      if (!node.children || node.children.length === 0) {
        paths.push(path);
        return;
      }

      for (const childNode of node.children) {
        traverse(childNode, path + "/");
      }
    }

    for (const node of hierarchy) {
      traverse(node, "");
    }

    return paths;
  };
  const targetRoutes = extractPathsFromHierarchy(menu);
  if (targetRoutes.some((v) => v.includes("$")))
    return <Outlet context={{ props: "xxx" }} />;
  if (
    targetRoutes.includes(pathname) ||
    whiteList.some((item) => pathname.includes(item))
  ) {
    return <Outlet />;
  } else {
    return (
      <Result
        status={403}
        title="403"
        subTitle="对不起, 你暂无权限访问此页面."
        extra={
          <Button type="primary" onClick={() => history.replace("/home")}>
            首页
          </Button>
        }
      />
    );
  }
};

export default PageAuth;
